package com.example.capsule_chat0322.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.adapter.FriendMoreFunctionAdapter;
import com.example.capsule_chat0322.bean.FriendApplyBean;
import com.example.capsule_chat0322.bean.FunctionBean;
import com.example.capsule_chat0322.connect.PositionOperation;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.interfaces.FriendFunctionItemOnClickInterface;
import com.example.capsule_chat0322.request.FriendRequest;
import com.google.gson.Gson;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/15 20:14
 */
public class FriendMoreFunctionActivity extends AppCompatActivity {

    private List<FunctionBean> functionBeanList;
    private RecyclerView friendFunctionRecyclerView;
    private FriendMoreFunctionAdapter friendMoreFunctionAdapter;
    private String userId;
    private static final int UPDATE_Friend_CODE = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_more_function);
        initView();//初始化UI控件
    }

    private void initView() {

        friendFunctionRecyclerView = findViewById(R.id.more_function_recycler_view);

        userId = (String) GlobalAssistMap.getObject("userId");

        if (userId == null) {
            return;
        }

        FriendMoreFunctionAdapter adapter = (FriendMoreFunctionAdapter) GlobalAssistMap.getObject("friendMoreFunctionAdapter");

        GlobalAssistMap.putObject("friendFunctionRecyclerView", friendFunctionRecyclerView);

        if (adapter == null) {
            doGetFunctionList();
        } else {
            LinearLayoutManager layoutManager = new LinearLayoutManager(FriendMoreFunctionActivity.this);
            friendFunctionRecyclerView.setLayoutManager(layoutManager);
            adapterAddListener(adapter);
            friendFunctionRecyclerView.setAdapter(adapter);
        }


    }

    private void doGetFunctionList() {
        FriendRequest.getFriendFunctionList(new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {

                List<FunctionBean> functionBeans = (List<FunctionBean>) message.obj;
                if (functionBeans != null) {
                    functionBeanList = functionBeans;
                    LinearLayoutManager layoutManager = new LinearLayoutManager(FriendMoreFunctionActivity.this);
                    friendFunctionRecyclerView.setLayoutManager(layoutManager);
                    friendMoreFunctionAdapter = new FriendMoreFunctionAdapter(functionBeanList);
                    adapterAddListener(friendMoreFunctionAdapter);
                    friendFunctionRecyclerView.setAdapter(friendMoreFunctionAdapter);
                    GlobalAssistMap.putObject("friendMoreFunctionAdapter", friendMoreFunctionAdapter);
                }

                return true;
            }
        }), userId);

    }


    public void adapterAddListener(FriendMoreFunctionAdapter friendMoreFunctionAdapter) {

        if (friendMoreFunctionAdapter == null) {
            return;
        }


        friendMoreFunctionAdapter.setFunctionItemOnClickInterface(new FriendFunctionItemOnClickInterface() {


            @Override
            public void onItemClick(RecyclerView parent, View view, int position, FunctionBean functionBean) {

                Intent intent = new Intent();
                if (functionBean.getFunctionName().equals("好友申请")) {

                    TextView count = view.findViewById(R.id.message_count);
                    count.setVisibility(View.GONE);
                    List<FriendApplyBean> applyList = functionBean.getApplyList();
                    intent.putExtra("applyList", new Gson().toJson(applyList));
                    intent.setClass(FriendMoreFunctionActivity.this, FriendApplyActivity.class);

                    FriendMoreFunctionAdapter adapter = (FriendMoreFunctionAdapter) GlobalAssistMap.getObject("friendMoreFunctionAdapter");
                    if (adapter != null) {
                        functionBeanList = adapter.functionBeanList;
                    }

                    functionBeanList.get(position).setApplyListStatus("1");
                    FriendMoreFunctionAdapter friendMoreFunctionAdapter = new FriendMoreFunctionAdapter(functionBeanList);
                    GlobalAssistMap.putObject("friendMoreFunctionAdapter", friendMoreFunctionAdapter);

                    startActivityForResult(intent, UPDATE_Friend_CODE);
                } else if (functionBean.getFunctionName().equals("添加好友")) {
                    PositionOperation.getLocation(FriendMoreFunctionActivity.this, new Handler(new Handler.Callback() {
                        @Override
                        public boolean handleMessage(@NonNull Message message) {
                            return true;
                        }
                    }));

                }

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null) {

            if (resultCode == UPDATE_Friend_CODE) {

                Intent intent = new Intent();
                setResult(UPDATE_Friend_CODE, intent);

            }

        }

    }
}
